<%@ page language="java" pageEncoding="UTF-8" import="ll.sms.pojo.*,ll.sms.util.*"%>
<%@page import="java.util.List;"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>代理注册</title>
		<link rel="stylesheet" type="text/css" href="../ext/ext-all.css" />
		<script type="text/javascript" src="../ext/ext-base.js"></script>
		<script type="text/javascript" src="../ext/ext-all.js"></script>
<%
//List<ChannelCombox> list = SmsUtil.getChannelList();
String defaulChannel = SmsUtil.sessionHelper(session,AdminProperty.adefaultchannel);

String notAuthoChannel = ExtJsonBuilder.getChannelReader(SmsUtil.getAllChannels());
//String authoChannnel = ExtJsonBuilder.getChannelReader(SmsUtil.getProxyChannelByDefaultChannel(defaulChannel));

%>
<script type="text/javascript">
Ext.BLANK_IMAGE_URL = '../images/s.gif';
</script>
	<script type="text/javascript">
	Ext.apply(Ext.form.VTypes, {
	password : function(val, field) {
        if (field.initialPassField) {
            var pwd = Ext.getCmp(field.initialPassField);
            return (val == pwd.getValue());
        }
        return true;
    },
    passwordText : '密码不匹配'
});
	Ext.onReady(function(){
		Ext.QuickTips.init();
		var wi = 600;
//****************************************************************
//****************************************************************
//****************************************************************
	var button = Ext.get('show-btn');

    var myData = {records : <%=notAuthoChannel%>};

	var myData1 = {records : []};


	// Generic fields array to use in both store defs.
	var fields = [
	   {name: 'name', mapping : 'name'},
	   {name: 'defaultname', mapping : 'defaultname'},
	   {name: 'userdname', mapping : 'userdname'}
	];

    // create the data store
    var firstGridStore = new Ext.data.JsonStore({
        fields : fields,
		data   : myData,
		root   : 'records'
    });


	// Column Model shortcut array
	var cols = [
		{header: "服务器名", width: 160, sortable: true, dataIndex: 'userdname'},
		{header: "服务器默认名", width: 160, sortable: true, dataIndex: 'defaultname'},
		{ id : 'name', header: "服务器ID", width: 10, sortable: true, dataIndex: 'name'}
	];

	// declare the source Grid
    var firstGrid = new Ext.grid.GridPanel({
		ddGroup          : 'secondGridDDGroup',
        store            : firstGridStore,
        columns          : cols,
		enableDragDrop   : true,
        stripeRows       : true,
        autoExpandColumn : 'name',
        width            : wi/2,
		region           : 'west',
        title            : '未授权通道'
    });

    var secondGridStore = new Ext.data.JsonStore({
        fields : fields,
        data   : myData1,
		root   : 'records'
    });

    // create the destination Grid
    var secondGrid = new Ext.grid.GridPanel({
		ddGroup          : 'firstGridDDGroup',
        store            : secondGridStore,
        columns          : cols,
		enableDragDrop   : true,
        stripeRows       : true,
        autoExpandColumn : 'name',
        width            : wi/2,
		region           : 'center',
        title            : '已授权通道'
    });


	//Simple 'border layout' panel to house both grids

	var displayPanel = new Ext.Window({
		title    : '通道配置',
		width    : wi,
		height   : 400,
		layout   : 'border',
		applyTo : 'panel123',
		closeAction :'hide',
		items    : [
			firstGrid,
			secondGrid
		]
		,
		bbar    : [
			{
				text    : '重置所有',
				handler : function() {
					firstGridStore.loadData(myData);
					secondGridStore.loadData(myData1);
				}
			},'-',
			{
				text    : '关闭',
				handler : function() {
					displayPanel.hide();
				}
			}
			,'->',
			{
			text    : '确定',
			handler : function() {
					doset();
				}
			}

		]
	});

	// used to add records to the destination stores
	var blankRecord =  Ext.data.Record.create(fields);

	/****
	* Setup Drop Targets
	***/
	// This will make sure we only drop to the view container
	var firstGridDropTargetEl =  firstGrid.getView().el.dom.childNodes[0].childNodes[1];
	var firstGridDropTarget = new Ext.dd.DropTarget(firstGridDropTargetEl, {
		ddGroup    : 'firstGridDDGroup',
		copy       : true,
		notifyDrop : function(ddSource, e, data){

			// Generic function to add records.
			function addRow(record, index, allItems) {

				// Search for duplicates
				var foundItem = firstGridStore.find('name', record.data.name);
				// if not found
				if (foundItem  == -1) {
					firstGridStore.add(record);

					// Call a sort dynamically
					firstGridStore.sort('name', 'ASC');

					//Remove Record from the source
					ddSource.grid.store.remove(record);
				}
			}

			// Loop through the selections
			Ext.each(ddSource.dragData.selections ,addRow);
			return(true);
		}
	});


	// This will make sure we only drop to the view container
	var secondGridDropTargetEl = secondGrid.getView().el.dom.childNodes[0].childNodes[1]

	var destGridDropTarget = new Ext.dd.DropTarget(secondGridDropTargetEl, {
		ddGroup    : 'secondGridDDGroup',
		copy       : false,
		notifyDrop : function(ddSource, e, data){

			// Generic function to add records.
			function addRow(record, index, allItems) {

				// Search for duplicates
				var foundItem = secondGridStore.find('name', record.data.name);
				// if not found
				if (foundItem  == -1) {
					secondGridStore.add(record);
					// Call a sort dynamically
					secondGridStore.sort('name', 'ASC');

					//Remove Record from the source
					ddSource.grid.store.remove(record);
				}
			}
			// Loop through the selections
			Ext.each(ddSource.dragData.selections ,addRow);
			return(true);
		}
	});

	var showproxy =new Ext.Button({
	        // applyTo:'login-button-group',
	         text: '配置代理',

	         handler: function()
		         {
					displayPanel.show();
		         }
		     	});

//****************************************************************
//****************************************************************
//****************************************************************

	    var simple = new Ext.FormPanel({
	    renderTo:'myf',
        labelWidth: 100, // label settings here cascade unless overridden
        url:'../ajaxjsp/proxyregdit.jsp',
        frame:true,
        title: '代理注册',
        bodyStyle:'padding:5px 5px 0',
        width: '80%',
        defaults: {width: 230},
        defaultType: 'textfield',
        labelAlign: 'right',
        waitMsgTarget: true,

        items: [
           {
                fieldLabel: '管理员用户名',
                name: 'auname',
                vtype:'alphanum',
                vtypeText:'用户名只能包括字母或者数字',
                allowBlank:false,

                blankText :'管理员用户名必须填写'
            },{
                fieldLabel: '密码',
                name: 'apwd',
                vtype:'alphanum',
               //inputType : 'password',
                vtypeText:'密码只能包括字母或者数字',
                allowBlank:false,
                id:'apwd',
                blankText :'密码必须填写'
            },{
                fieldLabel: '重新输入密码',
                name: 'reapwd',
                vtype:'alphanum',
                //inputType : 'password',
                vtypeText:'密码只能包括字母或者数字',
                allowBlank:false,
                initialPassField: 'apwd',
                blankText :'密码必须填写'

            },{
                fieldLabel: '代理公司名',
                name: 'acompany',
                allowBlank:false,
                blankText :'代理公司名必须填写'
            },{
                fieldLabel: '公司地址',
                name: 'aaddress'
            },{
                fieldLabel: '公司电话',
                name: 'aphone'
            },{
                fieldLabel: '联系人',
                name: 'alinkman',
                allowBlank:false,
                blankText :'联系人必须填写'
            },{
                fieldLabel: '联系人电话',
                name: 'alinkmanphone',
                allowBlank:false,
                blankText :'联系人电话必须填写'
            },{
                fieldLabel: '联系人QQ',
                name: 'alinkmanqq'
            },
            {
                fieldLabel: '可发短信数',
                name: 'asmsnum',
                xtype:'numberfield',
                allowBlank:false,
                blankText :'可发短信数必须填写'
            },{
                fieldLabel: '是否可用',
                name: 'aavailable',
                xtype:'hidden'
            },{
                fieldLabel: '已配置代理',
                name: 'adefaultchannel',
                id : 'adefaultchannel',
                xtype:'hidden'
            },showproxy
            ],

        buttons: [{
            text: '注册',
            handler : function(){
            //alert(Ext.get('checkbox').getValue());
             
           	var v = simple.getForm().findField('adefaultchannel').getValue();
			if(Ext.isEmpty(v)){
			Ext.MessageBox.confirm('警告', '你没有对代理配置通道，你确定不配置通道?',dosetchannel);
			}else{
			dosubmit();
			}

			function dosetchannel(btn){
				if(btn=='yes'){
					dosubmit();
				}else{
					displayPanel.show();
				}
			}
			
			function dosubmit(){
			 simple.form.submit({

					waitMsg : '正在处理......',
					success : function(form, action) {
            	        simple.form.reset();
						Ext.Msg.alert('成功',action.result.msg);
					},
					failure : function(form, action) {
						if (action.failureType == Ext.form.Action.SERVER_INVALID){
							Ext.MessageBox.alert('错误',action.result.errors.msg);
							}else{
							Ext.MessageBox.alert('错误','--------非法输入-------');
							}
					}
				});
			}

            }
        },{
            text: '重填',
            handler : function(){
          		simple.form.reset();
            }
        }]
    });


	function doset(){
	var store = secondGrid.getStore();
	var count = store.getCount();
	var value='';
	for(var i = 0 ; i<count ;i++){
		value = value + store.getAt(i).get('name');
		if(i!=count-1){
		value = value+',';
		}
	}
	simple.getForm().findField('adefaultchannel').setValue(value);
	displayPanel.hide();
	}

    });
	</script>

  </head>

  <body bgcolor="#ffffff">
 <br>
      <div id="panel123"></div>
     <div id="myf" align="center"></div>

    <input id="sel" type="hidden"/>
  </body>
</html>
